package xyz.scootaloo.oj.leetcode

import org.junit.Test
import xyz.scootaloo.common.TestDsl

/**
 * @author flutterdash@qq.com
 * @since 2021/7/10 10:01
 */
class P387 : TestDsl {

    private fun firstUniqChar(s: String): Int {
        val counts = IntArray(26)
        val indices = IntArray(26)

        for (i in s.indices) {
            val pos = s[i] - 'a'
            counts[pos]++
            if (counts[pos] == 1) {
                indices[pos] = i
            }
        }

        val list = counts
            .mapIndexed { idx, num ->  idx to num }
            .filter { pair ->  pair.second == 1 }
            .map { pair -> indices[pair.first] }
            .sorted()
        return if (list.isEmpty())
            -1
        else
            list[0]
    }

    @Test
    fun test0() {
        firstUniqChar("leetcode") shouldBe 0
        firstUniqChar("loveleetcode") shouldBe 2
    }

}
